home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / cppcom.com / MODEM.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-14  |  3.7 KB  |  163 lines

  1. /***************************************************************************
  2. These C++ classes are copyright 1990, by William Herrera.
  3. All those who put this code or its derivatives in a commercial product MUST
  4. mention this copyright in their documentation for users of the products in
  5. which this code or its derivative classes are used.  Otherwise, this code
  6. may be freely distributed and freely used for any purpose.
  7.  
  8. Enhancements: 1991 by David Orme
  9.     *  General cleanup.
  10.             - I/O now takes advantage of C++ overloading.
  11.             - Serial port I/O functionality now only in Serial class.
  12.             - Modem functionality now only in Modem class.
  13.     *  Possible to easily implement file Xfr prots now.
  14.     *  CCITT CRC-16 class added                            -- 2-20-1991
  15.     *  BIOS Timer class added                                -- 2-22-1991
  16.     *  Optional timeout on all input routines added    -- 2-25-1991
  17.  
  18. ***************************************************************************/
  19.  
  20. // file modem.cpp class definitions for modem class.
  21.  
  22. #include <string.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25.  
  26. #include "modem.hpp"
  27.  
  28. const char inits[] = "AT &Q0 &Q3 M1 S7=60 S11=55 V1 S0=6";
  29. const char tonedials[] = "ATDT";
  30. const char pulsedials[] = "ATDP";
  31. const char answers[] = "ATA";
  32. const char hangups[] = "ATH0";
  33. const char resets[] = "ATZ";
  34. const char retrns[] = "\r\n";
  35. const char escapes[] = "+++";    
  36. const char defaultcfgs[] = "modem.cfg";
  37.  
  38. modem::modem(int portnum, int speed, parity_t p,
  39.                          int sbits, int dbits, boolean trans)
  40.  : SerialPort(portnum, speed, p, sbits, dbits, trans)
  41. {
  42.     init = strdup(inits);
  43.     tonedial = strdup(tonedials);
  44.     pulsedial = strdup(pulsedials);
  45.     answer = strdup(answers);
  46.     hangup = strdup(hangups);
  47.     reset = strdup(resets);
  48.     retrn = strdup(retrns);
  49.     escape = strdup(escapes);
  50. }
  51.  
  52. modem::~modem()
  53. {
  54.     free(init);
  55.     free(tonedial);
  56.     free(pulsedial);
  57.     free(answer);
  58.     free(hangup);
  59.     free(reset);
  60.     free(retrn);
  61.     free(escape);
  62. }
  63.  
  64. void modem::ConfigureFromFile(const char * pathname)
  65. {
  66.     char ini[129], ton[129], pul[129], ans[129], han[129];
  67.     char res[129], ret[129], esc[129], actualret[129];
  68.     FILE * cfg = fopen(pathname, "r");
  69.     if(cfg == NULL)
  70.         fprintf(stderr, "modem error:  cannot open or use"
  71.                         " config file %s\n",    pathname);
  72.     int n = fscanf(cfg, "%128[^\n]\n%128[^\n]\n%128[^\n]"
  73.                         "%128[^\n]\n%128[^\n]\n%128[^\n]"
  74.                         "%128[^\n]%128[^\n]",
  75.                         ini, ton, pul, ans, han, res, ret, esc);
  76.     if(n != 8)
  77.         fprintf(stderr, "Error in configuration file %s\n", pathname);
  78.     else
  79.     {
  80.         char * p = ret;
  81.         char * q = actualret;
  82.         do 
  83.         {
  84.             if(*p == '\\')
  85.             {
  86.                 ++p;
  87.                 switch(*p)
  88.                 {
  89.                     case 'r': *p = '\r'; break;
  90.                     case 'n': *p = '\n'; break;
  91.                     case 't': *p = '\t'; break;
  92.                     case 'f': *p = '\f'; break;
  93.                     case 'v': *p = '\v'; break;
  94.                     case 'a': *p = '\a'; break;
  95.                     default: break;
  96.                 }
  97.             }
  98.             *q++ = *p;
  99.         } while (*p++);
  100.         free(init);
  101.         free(tonedial);
  102.         free(pulsedial);
  103.         free(answer);
  104.         free(hangup);
  105.         free(reset);
  106.         free(retrn);
  107.         free(escape);
  108.  
  109.         init = strdup(ini);
  110.         tonedial = strdup(ton);
  111.         pulsedial = strdup(pul);
  112.         answer = strdup(ans);
  113.         hangup = strdup(han);
  114.         reset = strdup(res);
  115.         retrn = strdup(actualret);
  116.         escape = strdup(esc);
  117.  
  118.         Initialize();
  119.     }
  120.     return;
  121. }
  122.  
  123. void modem::Dial(char * number, boolean tone)
  124. {
  125.     Send( (tone) ? tonedial : pulsedial);
  126.     Send(number);
  127.     Send(retrn);
  128. }
  129.  
  130. void modem::Initialize()
  131. {
  132.     Send(init);
  133.     Send(retrn);
  134. }
  135.  
  136. void modem::Answer()
  137. {
  138.     Send(answer);
  139.     Send(retrn);
  140. }
  141.  
  142. void modem::Escape()
  143. {
  144.     Send(escape);
  145.     Pause(3000);
  146. }
  147.  
  148. void modem::HangUp()
  149. {
  150.     Send(hangup);
  151.     Send(retrn);
  152. }
  153.  
  154. void modem::SendCommand(char * command)
  155. {
  156.     Send(command);
  157.     Send(retrn);
  158. }
  159.  
  160.  
  161. // end of file modem.cpp
  162.  
  163.